/**
 * Created by gongxun on 16/12/19.
 */

var SoundHelper = function(){
    var that = {};

    that.audioEngine = cc.audioEngine;

    that.localSound = "";

    //只有在退出的时候恢复
    that.stopPlayerMusicFile = null;

    that.setMusicVolume = function(music){
        that.audioEngine.setMusicVolume(music / 100);
    };

    that.setEffectsVolume = function(effects){
        that.audioEngine.setEffectsVolume(effects / 100);
    };

    that.onGameHide = function(){
        that.audioEngine.stopMusic();
        cc.log("onGameHide");
    };

    that.onGameShow = function(){
        if (that.stopPlayerMusicFile != null){
            var music = that.stopPlayerMusicFile;
            that.stopPlayerMusicFile = null;
            that.playMusicForFile(music);
        }
        cc.log("onGameShow");

        // that.audioEngine.resumeMusic();

        // return;
    };

    that.playMusicForFile = function(musicFile){
        if (musicFile == that.stopPlayerMusicFile){
            return;
        }

        that.audioEngine.playMusic(musicFile, true);
        that.stopPlayerMusicFile = musicFile;
    };

    that.playMusicHall = function(){
        that.playMusicForFile("res/sound/playingInHall.mp3");
    };

    that.playMusicGame = function(){
        that.playMusicForFile("res/sound/playingInGame.mp3");
    };

    that.playMusicGameNn = function(){
        that.playMusicForFile("res/sound/bgm_game_1.mp3");
    };

    that.playMusicGameGsbdx = function(){
        that.playMusicForFile("res/sound/bgm_game_1.mp3");
    };

    that.playMusicGamePsz = function(){
        that.playMusicForFile("res/sound/bgm_game_1.mp3");
    };

    that.playMusicGameWhmj = function(){
        that.playMusicForFile("res/sound/playingInGame.mp3");
    };

    that.playMusicGameDzpk = function(){
        that.playMusicForFile("res/sound/bgm_game_1.mp3");
    };

    that.playMusicGameDdz = function(){
        that.playMusicForFile("res/sound/bgm_game_1.mp3");
    };

    that.playMusicGamePdk = function(){
        that.playMusicForFile("res/sound/bgm_game_card.mp3");
    };

    that.stopMusic = function(){
        that.stopPlayerMusicFile = null;
        that.audioEngine.stopMusic();
    };

    that.playEffect = function(file){
        var soundId = that.audioEngine.playEffect(file);
    };

    that.getSexStr = function(sex){
        return sex == 0 ? "boy" : "girl";
    };

    that.playeKaichangbai = function(){
        if (mo.gameHelper.projectCode == 0){
            var file = "res/sound/kaichangbai.mp3";
            that.playEffect(file);
        }
    };

    that.playIntoHallEffect = function(){
        var file = "res/sound/effect/into_hall.mp3";
        that.playEffect(file);
    };

    that.playButtonClick = function(){
        var file = "res/sound/effect/click_btn.mp3";
        that.playEffect(file);
    };

    that.playMenuOpenClick = function(){
        var file = "res/sound/effect/btton_menuOpen.mp3";
        that.playEffect(file);
    };

    that.playMenuCloseClick = function(){
        var file = "res/sound/effect/button_menuClose.mp3";
        that.playEffect(file);
    };

    that.playDialogOpen = function(){
        var file = "res/sound/effect/dialogOpen.mp3";
        that.playEffect(file);
    };

    that.playMjSelect = function(){
        var file = "res/sound/effect/select.mp3";
        that.playEffect(file);
    };

    that.playMjPengEffect = function(){
        var file = "res/sound/effect/peng.mp3";
        that.playEffect(file);
    };

    that.playMjDeal = function(){
        var file = "res/sound/effect/deal.mp3";
        that.playEffect(file);
    };

    that.playMjWin = function(){
        var file = "res/sound/effect/win.mp3";
        that.playEffect(file);
    };

    that.playMjLose = function(){
        var file = "res/sound/effect/loss.mp3";
        that.playEffect(file);
    };

    that.playMjLiuju = function(){
        var file = "res/sound/effect/liuju.mp3";
        that.playEffect(file);
    };

    that.playStartGame = function(){
        var file = "res/sound/effect/startGame.mp3";
        that.playEffect(file);
    };

    that.playMjMaima = function(){
        var file = "res/sound/effect/maima.mp3";
        that.playEffect(file);
    };

    that.playMjCard = function(cardNum, sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/card_" + cardNum + ".mp3";
        that.playEffect(file);
    };

    that.playMjPeng = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/open_peng.mp3";
        that.playEffect(file);
    };

    that.playMjGang = function(sex, type){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/open_gang.mp3";
        that.playEffect(file);
    };

    that.playMjLiang = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/open_liang.mp3";
        that.playEffect(file);
    };

    that.playMjHu = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/open_hu.mp3";
        that.playEffect(file);
    };

    that.playMjZimo = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/open_hu.mp3";
        that.playEffect(file);
    };

    that.playMjHuType = function(sex, hutype){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/hu_winType" + hutype + ".mp3";
        that.playEffect(file);
    };

    that.playUserCustom = function(sex, index){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/mj/" + that.localSound + "/" + sexStr + "/chat_" + index + ".mp3";
        that.playEffect(file);
    };

    that.playHuType = function(sex, isZimo, winType){
        ///播放胡牌相关
        //优先级 大胡 > 自摸 > 平胡
        //本地存在的音效
        var isEsixtEffect = [
            2,
            4,
            8,
            16,
            64,
            128,
            256,
            512,
            1024,
            8192,
            65536
        ];

        var isPlayBig = false;
        for (var i = 16; i >= 0; i--){
            var temp = Math.pow(2, i);
            if ((temp & winType) != 0 && mo.gameHelper.findValueForArray(isEsixtEffect, temp) != null){
                isPlayBig = true;
                that.playMjHuType(sex, temp);
                break;
            }
        }

        if (!isPlayBig){
            if (isZimo){
                that.playMjZimo(sex);
            }else{
                that.playMjHu(sex);
            }
        }
    };

    //---------------------------------
    //whmj
    //---------------------------------

    that.playMjCardWhmj = function(cardNum, sex){
        var cardNum_sound_count = [
            3,1,2,2,2,1,2,2,2,
            4,2,2,2,2,1,2,3,2,
            2,2,1,1,1,1,1,2,2,
            2,2,2,3,2,2,4
        ];

        var index = (parseInt(cardNum / 20) - 1) * 9 + cardNum % 20 - 1;
        var count = cardNum_sound_count[index];

        var sexStr = that.getSexStr(sex);
        var file = "res/sound/whmj/mj/" + sexStr + "/mj/card_" + cardNum + "_" + parseInt(Math.random() * count + 1) + ".mp3";
        that.playEffect(file);
    };

    that.playMjPengWhmj = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/whmj/mj/" + sexStr + "/caozuo/Peng" + parseInt(Math.random() * 4 + 1) + ".mp3";
        that.playEffect(file);
    };

    that.playMjChiWhmj = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/whmj/mj/" + sexStr + "/caozuo/Chi" + parseInt(Math.random() * 3 + 1) + ".mp3";
        that.playEffect(file);
    };


    //4 明杠 4张
    //5 暗杠 4张
    //6 补杠 1张
    //7 单杠 1张

    that.playMjGangWhmj = function(sex, type, cardNum){
        var sexStr = that.getSexStr(sex);
        var gangType = "";
        if (type == 4){
            gangType = "Gang1";
        }else if (type == 5){
            gangType = "AnGang1";
        }else if (type == 6){
            gangType = "Gang1";
        }else if(type == 7){
            if(cardNum == 85){ //红中
                gangType = "HongZhong1";
            }else if(cardNum == 86){
                gangType = "FaCai1";
            }else if(mo.whmjCardHelper.getIsLaizi(cardNum)){
                gangType = "LaiZi1";
            }else if(mo.whmjCardHelper.getIsPizi(cardNum)){
                gangType = "PiGang1";
            }
        }
        var file = "res/sound/whmj/mj/" + sexStr + "/caozuo/" + gangType + ".mp3";
        that.playEffect(file);
    };

    that.playMjHuWhmj = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/whmj/mj/" + sexStr + "/caozuo/XiaoHu1" + parseInt(Math.random() * 2 + 1) + ".mp3";
        that.playEffect(file);
    };

    that.playMjZimoWhmj = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/whmj/mj/" + sexStr + "/caozuo/ZiMo1"  + ".mp3";
        that.playEffect(file);
    };

    that.playMjHuTypeWhmj = function(sex, hutype){
        //todo 武汉麻将各种大胡的音效
        // var sexStr = that.getSexStr(sex);
        // var file = "res/sound/" + that.localSound + "/" + sexStr + "/hu_winType" + hutype + ".mp3";
        // that.playEffect(file);
    };


    that.playHuTypeWhmj = function(sex, isZimo, winType){
        if(isZimo){
            that.playMjZimoWhmj(sex);
        }else{
            that.playMjHuWhmj(sex);
        }
    };


    //---------------------------------
    //nn
    //---------------------------------

    that.playNnCardType = function(sex, cardType){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/nn/" + sexStr + "/nn_" + cardType + ".mp3";
        that.playEffect(file);
    };

    that.playNnSelect = function(isSelect){
        var file = "";
        if (isSelect){
            file = "res/sound/nn/select_card.mp3";
        }else{
            file = "res/sound/nn/unselect_card.mp3";
        }
        that.playEffect(file);
    };

    that.playNnFapai = function(){
        var file = "res/sound/nn/panel.mp3";
        that.playEffect(file);
    };

    that.playNnGetCoins = function(){
        var file = "res/sound/nn/gain_chips.mp3";
        that.playEffect(file);
    };

    that.playNnWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/nn/win.mp3";
        }else{
            file = "res/sound/nn/lose.mp3";
        }
        that.playEffect(file);
    };

    //---------------------------------
    //gsbdx
    //---------------------------------
    that.playGsbdxCardType = function(sex, cardNums){
        var sexStr = that.getSexStr(sex);
        // var cardType = mo.gsbdxTableStatus.getCardType(cardNums);

        var file = "res/sound/ttz/" + sexStr + "/" + mo.gsbdxCardHelper.getCardTypeSoundFile(cardNums);
        that.playEffect(file);
    };

    that.playGsbdxFapai = function(){
        var file = "res/sound/ttz/card_shuffle.mp3";
        that.playEffect(file);
    };

    that.playGsbdxGetCoins = function(){
        var file = "res/sound/nn/gain_chips.mp3";
        that.playEffect(file);
    };

    that.playGsbdxWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/nn/win.mp3";
        }else{
            file = "res/sound/nn/lose.mp3";
        }
        that.playEffect(file);
    };

    that.playGsbdxRockDice = function(){
        var file = "res/sound/ttz/rock_sezi.mp3";
        that.playEffect(file);
    };

    that.playGsbdxOpenCard = function(){
        var file = "res/sound/ttz/card_open.mp3";
        that.playEffect(file);
    };

    that.playGsbdxSendCard = function(){
        var file = "res/sound/ttz/ttz_card_send.mp3";
        that.playEffect(file);
    };

    that.playGsbdxShuffleCard = function(){
        var file = "res/sound/ttz/card_shuffle.mp3";
        that.playEffect(file);
    };

    that.playGsbdxTimeRun = function(){
        var file = "res/sound/ttz/time_run.mp3";
        that.playEffect(file);
    };

    that.playGsbdxTimeOut = function(){
        var file = "res/sound/ttz/time_out.mp3";
        that.playEffect(file);
    };

    that.playGsbdxMDLS = function(){
        var file = "res/sound/ttz/maidinglishou.mp3";
        that.playEffect(file);
    };

    //---------------------------------
    //psz
    //---------------------------------
    that.playPszWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/nn/win.mp3";
        }else{
            file = "res/sound/nn/lose.mp3";
        }
        that.playEffect(file);
    };

    that.playEffectAddChips = function(){
        var file = "res/sound/psz/addChips.mp3";
        that.playEffect(file);
    };

    that.playEffectCompare = function(){
        var file = "res/sound/psz/compareCard.mp3";
        that.playEffect(file);
    };

    that.playEffectFollow = function(){
        var file = "res/sound/psz/followChips.mp3";
        that.playEffect(file);
    };

    that.playEffectGiveUp = function(){
        var file = "res/sound/psz/giveUp.mp3";
        that.playEffect(file);
    };

    that.playEffecLookCard = function(){
        var file = "res/sound/psz/lookCard.mp3";
        that.playEffect(file);
    };

    that.playEffectWinCard = function(){
        var file = "res/sound/psz/winChips.mp3";
        that.playEffect(file);
    };

    that.playPszPeopleBipai = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/psz/" + sexStr + "/bipai.mp3";
        that.playEffect(file);
    };

    that.playPszPeopleFangqi = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/psz/" + sexStr + "/giveUp.mp3";
        that.playEffect(file);
    };

    that.playPszPeopleGenzhu = function(sex){
        var sexStr = that.getSexStr(sex);
        var index = parseInt(Math.random() * 3);
        var file = "res/sound/psz/" + sexStr + "/genzhu" + index + ".mp3";
        that.playEffect(file);
    };

    that.playPszPeopleJiazhu = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/psz/" + sexStr + "/jiazhu.mp3";
        that.playEffect(file);
    };

    that.playPszPeopleKanpai = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/psz/" + sexStr + "/kanpai.mp3";
        that.playEffect(file);
    };

    //dzpk
    that.playDzpkDealCard = function () {
        var file = "res/sound/dzpk/deal_card.mp3";
        that.playEffect(file);
    };

    that.playDzpkCall = function (sex) {
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/dzpk/call_" + sexStr + "1.mp3";
        that.playEffect(file);
    };

    that.playDzpkRaise = function (sex) {
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/dzpk/raise_" + sexStr + ".mp3";
        that.playEffect(file);
    };

    that.playDzpkCheck = function (sex) {
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/dzpk/check_" + sexStr + ".mp3";
        that.playEffect(file);
    };

    that.playDzpkFold = function (sex) {
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/dzpk/" + sexStr + "_giveup1.mp3";
        that.playEffect(file);
    };

    that.playDzpkAllin = function (sex) {
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/dzpk/allin_" + sexStr + ".mp3";
        that.playEffect(file);
    };

    that.playDzpkWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/nn/win.mp3";
        }else{
            file = "res/sound/nn/lose.mp3";
        }
        that.playEffect(file);
    };

    //dtj
    that.playDtjSelect = function(isSelect){
        var file = "";
        if (isSelect){
            file = "res/sound/dtj/select_card.mp3";
        }else{
            file = "res/sound/dtj/unselect_card.mp3";
        }
        that.playEffect(file);
    };

    that.playDtjWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/dtj/win.mp3";
        }else{
            file = "res/sound/dtj/lose.mp3";
        }
        that.playEffect(file);
    };

    that.playDtjGetCoins = function(){
        var file = "res/sound/dtj/gain_chips.mp3";
        that.playEffect(file);
    };

    that.playDtjCardType = function(sex, cardType){
        var sexStr = that.getSexStr(sex);

        var file = "res/sound/dtj/" + sexStr + "/dtj_" + cardType + ".mp3";
        that.playEffect(file);
    };

    that.playDtjRockDice = function(){
        var file = "res/sound/dtj/rock_sezi.mp3";
        that.playEffect(file);
    };

    that.playDtjSendCard = function(){
        var file = "res/sound/ttz/ttz_card_send.mp3";
        that.playEffect(file);
    };

    //pdk
    that.playPdkWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/pdk/win.mp3";
        }else{
            file = "res/sound/pdk/lose.mp3";
        }
        that.playEffect(file);
    };

    that.playPdkGetCoins = function(){
        var file = "res/sound/pdk/gain_chips.mp3";
        that.playEffect(file);
    };

    //要不起
    that.playPdkYaobuqi = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/pdk/" + sexStr + "/buyao.mp3";
        that.playEffect(file);
    };

    //保单
    that.playPdkBaodan = function(sex){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/pdk/" + sexStr + "/baoban.mp3";
        that.playEffect(file);
    };

    //播放输赢
    that.playPdkLoseWin = function(isWin){
        var file = "";
        if (isWin){
            file = "res/sound/pdk/MusicEx_Win.mp3";
        }else{
            file = "res/sound/pdk/MusicEx_Lose.mp3";
        }
        that.playEffect(file);
    };

    //播放牌型音效
    that.playPdkCardTypeSound = function(sex, cardType){
        var sexStr = that.getSexStr(sex);
        var file = "res/sound/pdk/" + sexStr + "/cardType_" + cardType + ".mp3";
        that.playEffect(file);
    };

    //播放单张
    that.playPdkSingle = function(sex, num){
        var playNum = 0;
        if (num == 53){
            playNum = 14;
        }else if (num == 54){
            playNum = 15;
        }else{
            playNum = (num - 1) % 13 + 1;
        }

        var sexStr = that.getSexStr(sex);
        var file = "res/sound/pdk/" + sexStr + "/" + playNum + ".mp3";
        that.playEffect(file);
    };

    //播放对子
    that.playPdkPair = function(sex, num){
        var playNum = 0;
        if (num == 53){
            return
        }else if (num == 54){
            return
        }else{
            playNum = (num - 1) % 13 + 1;
        }

        var sexStr = that.getSexStr(sex);
        var file = "res/sound/pdk/" + sexStr + "/dui" + playNum + ".mp3";
        that.playEffect(file);
    };

    //播放三张
    that.playPdkTriple = function(sex, num){
        var playNum = 0;
        if (num == 53){
            return
        }else if (num == 54){
            return
        }else{
            playNum = (num - 1) % 13 + 1;
        }

        var sexStr = that.getSexStr(sex);
        var file = "res/sound/pdk/" + sexStr + "/tuple" + playNum + ".mp3";
        that.playEffect(file);
    };

    //音效炸弹
    that.playPdkEffectBoom = function(){
        var file = "res/sound/pdk/effect_bomb.mp3";
        that.playEffect(file);
    };

    //音效打出
    that.playPdkEffectDeal = function(){
        var file = "res/sound/pdk/effect_deal.mp3";
        that.playEffect(file);
    };

    //音效飞机
    that.playPdkEffectPlane = function(){
        var file = "res/sound/pdk/effect_plane.mp3";
        that.playEffect(file);
    };

    //音效选牌
    that.playPdkEffectSelectCard = function(){
        var file = "res/sound/pdk/effect_selectCard.mp3";
        that.playEffect(file);
    };

    return that;
};

mo.soundHelper = new SoundHelper();